package edu.msg.jthr.web.servlets;

import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import edu.msg.jthr.backend.exception.ServiceException;
import edu.msg.jthr.backend.model.Candidate;
import edu.msg.jthr.backend.model.Comment;
import edu.msg.jthr.backend.model.Interview;
import edu.msg.jthr.backend.model.User;
import edu.msg.jthr.backend.service.CandidateService;
import edu.msg.jthr.backend.service.UserService;
import edu.msg.jthr.web.util.RoleAssembler;

/**
 * Servlet implementation class AddInterview
 */
@WebServlet("/AddInterview")
public class AddInterview extends HttpServlet {
	private static final long serialVersionUID = 1L;
	@EJB
	private UserService userService;
	@EJB
	private CandidateService candidateService;
	private HttpSession session;
	private static Logger LOG = LoggerFactory.getLogger(AddInterview.class);

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public AddInterview() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		session = request.getSession(true);
		User user = (User) session.getAttribute("activeUser");
		if (user == null) {
			user = userService.getUserByUserName(request.getUserPrincipal()
					.toString());
			String role = RoleAssembler.getRole(user.getUserGroups());
			session.setAttribute("role", role);
			session.setAttribute("activeUser", user);
		}
		String pagePath = "/ShowJobs";
		request.getRequestDispatcher(pagePath).forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		session = request.getSession(true);

		Interview interview = new Interview();
		try {
			String dateString = request.getParameter("date");
			DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
			Date date;
			date = formatter.parse(dateString);
			interview.setDate(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}

		User user = new User();
		user = userService
				.getById(Long.parseLong(request.getParameter("combo")));
		interview.setUser(user);
		Comment comment = new Comment();
		comment.setContent(request.getParameter("feedBack"));
		comment.setDate(Calendar.getInstance().getTime());
		comment.setUser(user);
		interview.setFeedBack(comment);

		Candidate candidate = (Candidate) session.getAttribute("candidate");
		interview.setCandidate(candidate);

		List<Interview> interviewList = Collections.emptyList();
		interviewList = candidate.getInterview();
		interviewList.add(interview);

		candidate.setInterview(interviewList);
		List<Candidate> candidateList = Collections.emptyList();
		try {
			candidateService.update(candidate);
			
			candidateList = candidateService.getAll();
		} catch(ServiceException e) {
			LOG.error(e.getMessage(), e);
		}
		
		
		session.setAttribute("candidateList", candidateList);
		request.getRequestDispatcher("/pages/ShowCandidates.jsp").forward(
				request, response);
	}

}
